package controller;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;

import main.Log;
import model.map.Direction;
import model.map.HexLocationProjection;
import model.map.Location;
import view.MapOverlay;
import view.View;

public class CameraController extends Controller {

	MapOverlay mo;
	JFrame gw;
	HexLocationProjection proj;
	
	public CameraController( MapOverlay mo, JFrame gw ) {
		this.mo = mo;
		this.gw = gw;
		this.proj = new HexLocationProjection();
		initiate();
	}
	
	@Override
	public void update(View v) {
		//TODO implement
	}
	
	private void initiate() {
		gw.addKeyListener( new KeyListener() {
			@Override
			public void keyPressed(KeyEvent arg0) {
				if( arg0.getKeyCode() == GameConfig.MOVE_SCREEN_UP ) {
					moveUp();
				}
			}
			@Override
			public void keyReleased(KeyEvent arg0) {}
			@Override
			public void keyTyped(KeyEvent arg0) {}
		});
		gw.addKeyListener( new KeyListener() {
			@Override
			public void keyPressed(KeyEvent arg0) {
				if( arg0.getKeyCode() == GameConfig.MOVE_SCREEN_RIGHT ) {
					moveRight();
				}
			}
			@Override
			public void keyReleased(KeyEvent arg0) {}
			@Override
			public void keyTyped(KeyEvent arg0) {}
		});
		gw.addKeyListener( new KeyListener() {
			@Override
			public void keyPressed(KeyEvent arg0) {
				if( arg0.getKeyCode() == GameConfig.MOVE_SCREEN_DOWN ) {
					moveDown();
				}
			}
			@Override
			public void keyReleased(KeyEvent arg0) {}
			@Override
			public void keyTyped(KeyEvent arg0) {}
		});
		gw.addKeyListener( new KeyListener() {
			@Override
			public void keyPressed(KeyEvent arg0) {
				if( arg0.getKeyCode() == GameConfig.MOVE_SCREEN_LEFT ) {
					moveLeft();
				}
			}
			@Override
			public void keyReleased(KeyEvent arg0) {}
			@Override
			public void keyTyped(KeyEvent arg0) {}
		});
	}
	
	private void moveUp() {
		Location n = proj.projectLocation( mo.getCenterLocation(), Direction.N );
		Log.out(Log.CONTROLLER, "from " + mo.getCenterLocation() + " to " + n);
		mo.setCenterLocation( n );
	}
	
	private void moveRight() {
		Location n = proj.projectLocation( mo.getCenterLocation(), Direction.E );
		Log.out(Log.CONTROLLER, "from " + mo.getCenterLocation() + " to " + n);
		mo.setCenterLocation( n );
	}
	
	private void moveDown() {
		Location n = proj.projectLocation( mo.getCenterLocation(), Direction.S );
		Log.out(Log.CONTROLLER, "from " + mo.getCenterLocation() + " to " + n);
		mo.setCenterLocation( n );
	}
	
	private void moveLeft() {
		Location n = proj.projectLocation( mo.getCenterLocation(), Direction.W );
		Log.out(Log.CONTROLLER, "from " + mo.getCenterLocation() + " to " + n);
		mo.setCenterLocation( n );
	}
	
}
